﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Core.Distribution;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.AppVersion;
using FwProject.Core.Car;
using FwProject.Car;
using FwProject.Core.FactoryInfo;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.TypeNumber;
using Castle.Core.Logging;
using Abp.ObjectMapping;
using Abp.ElasticSearch;
using FwProject.BrandInfo;
using System.Threading.Tasks;
using FwProject.BrandInfo.Dto;
using FwProject.ShoppingMall.Shop;
using FwProject.Shop.Dto;
using FwProject.Shop;
using FwProject.Comm;

namespace FwProject.ShopApplication.RemoteEventBus
{
    /// <summary>
    ///修改秒杀数据
    /// </summary>
    [RemoteEventHandler(ForType = RemoteTypeHelp.TYPEEDITSILL, ForTopic = RemoteTypeHelp.TOPICAPPSILLUPDATE)]
    public class RemoteEventBusEidtProduct : IRemoteEventHandler, ITransientDependency
    {
        public ICacheManager _catchmanager { get; set; }
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;
        public IElasticsearch _elasticsearch;
        public IShopProductAppService _shopProductAppService;
        public ILogger iLogger { get; set; }

        public IObjectMapper _objectMapper { get; set; }

        public RemoteEventBusEidtProduct(
              IRepository<ShopProduct, Guid> repositoryShopProduct,
             IElasticsearch elasticsearch,
             IShopProductAppService shopProductAppService
            )
        {
            _shopProductAppService = shopProductAppService;
            _elasticsearch = elasticsearch;
            _repositoryShopProduct = repositoryShopProduct;
            _objectMapper = NullObjectMapper.Instance;
          
        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            try
            {
                var model= _repositoryShopProduct.Get(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()));
                model.ShopProductFlag = ShopProductFlag.正常;
                ///同时更新缓存
                Task.Run(async () => await _elasticsearch.AddOrUpdateAsync<ShopProductListAppDto, Guid>(nameof(ShopProductListAppDto).ToLower(), _shopProductAppService.GetListAppDto(Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString()))));

            }
            catch (Exception exe)
            {
                iLogger.Info($"商城秒杀商品时间到了搜索引擎名称==={eventArgs.EventData.Data[RemoteTypeHelp.MODELNAME].ToString()}===搜索引擎生成错误", exe);
            }
        }
    }
}
